package com.zuxia.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    private List Token(){
        //添加head参数start
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("Authorization").description("请求接口所需Token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //false非必输 true必输
        pars.add(tokenPar.build());
        return pars;
    }

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/api/.*")))
                .build().globalOperationParameters(Token());
    }

    @Bean
    public Docket adminApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build().globalOperationParameters(Token());
    }

    @Bean
    public Docket userApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("userApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/user/.*")))
                .build().globalOperationParameters(Token());
    }

    @Bean
    public Docket dumpApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("dumpApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/dump/.*")))
                .build().globalOperationParameters(Token());
    }

    @Bean
    public Docket shopApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("shopApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/shop/.*")))
                .build().globalOperationParameters(Token());
    }

    @Bean
    public Docket groupApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("groupApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/group/.*")))
                .build().globalOperationParameters(Token());
    }

    @Bean
    public Docket orderApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("orderApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.and(PathSelectors.regex("/order/.*")))
                .build().globalOperationParameters(Token());
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站的API文档")
                .description("本文档描述了足下在线学习网站的api接口定义")
                .version("1.0")
                .contact(new Contact("laoli", "http://www.zuxia.com", "573925122@qq.com"))
                .build();
    }

    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理系统的API文档")
                .description("本文档描述了足下后台管理系统的api接口定义")
                .version("1.0")
                .contact(new Contact("laolo", "http://www.zuxia.com", "573925122@qq.com"))
                .build();
    }
}
